package com.yunkaweilai.android.view.dialog.shop;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.shop.ShopTypeActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;

import org.greenrobot.eventbus.EventBus;

/**
 * 添加小分类
 * Created by Administrator on 2016/12/22 0022.
 */

public class ShopTypeMinDialog extends Dialog {
    ImageView idImgClose;
    TextView idTvRight;
    EditText idEdtTypeName;
    TextView idTvUpType;
    private Context context;
    private String parent_id = "";//父级id
    private String parent_id_name = "";//父级id,名字
    private String type = "";
    public ShopTypeMinDialog(Context context,String parent_id,String parent_id_name,String type) {
        super(context,R.style.dialog);
        this.context = context;
        this.parent_id = parent_id;
        this.parent_id_name = parent_id_name;
        this.type = type;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_type_min_add);
        idImgClose = (ImageView)findViewById(R.id.id_img_close);
        idTvRight = (TextView) findViewById(R.id.id_tv_right);
        idTvUpType = (TextView) findViewById(R.id.id_tv_up_type);
        idEdtTypeName = (EditText) findViewById(R.id.id_edt_type_name);

        idTvUpType.setText("上级分类：" + parent_id_name);
        initview();
    }

    public void initview() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())) {
                    if (type.equals(ShopTypeActivity.PT_SHOP)) {
                        addType(Api.PT_SHOP_TYPE_SAVE);
                    } else if (type.equals(ShopTypeActivity.TIME_SHOP)) {
                        addType(Api.TIME_SHOP_TYPE_SAVE);
                    }
                }
            }
        });
    }

    private void addType(String url){
        HttpRequest.postUrl(url)
                .addParams("parent_id",parent_id)
                .addParams("goods_category_name",idEdtTypeName.getText().toString())
                .addParams("status","1")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MAX_TYPE,true));
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_LIST_REFRESH,true));
                        }
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(context,"添加分类失败");
                    }
                });

    }

}
